<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <TodoHeader @addList="addList"></TodoHeader>
    <!-- 主体部分 -->
    <TodoBody
      :list="renderList"
      @delList="delList"
      @change="change"
      @checkAll="checkAll"
    ></TodoBody>
    <!-- 底部部分 -->
    <TodoFooter :list="list" @clearList="clearList" :type="type" @filter="filter"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader.vue'
import TodoBody from './components/TodoBody.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  data () {
    return {
      type: 'all',
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true }
      ]
    }
  },
  components: {
    TodoHeader,
    TodoBody,
    TodoFooter
  },
  methods: {
    delList (id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    change (id) {
      const result = this.list.find(item => item.id === id)
      result.isDone = !result.isDone
    },
    addList (value) {
      const obj = { id: +new Date(), name: value, isDone: false }
      this.list = [obj, ...this.list]
    },
    clearList () {
      this.list = this.list.filter(item => item.isDone === false)
    },
    checkAll (value) {
      this.list.forEach(item => {
        item.isDone === value
      })
    },
    changeType (type) {
      this.type = type
    },
    filter(type){
      this.type = type
    }
  },
  computed:{
    renderList() {
      switch (this.type) {
        case 'all':
          return this.list
        case 'active':
          return this.list.filter((item) => !item.isDone)
        case 'completed':
          return this.list.filter((item) => item.isDone)
        default:
          return this.list
      }
    },
  }
}
</script>

<style></style>
